/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.start.controller;


import com.itgrail.pilot.app.command.cmo.AddOrUpdateRoleCmd;
import com.itgrail.pilot.app.command.cmo.DeleteRoleByIdCmd;
import com.itgrail.pilot.app.command.cmo.query.PageQueryRoleCmd;
import com.itgrail.pilot.app.command.cmo.query.QueryPerminByRoleCmd;
import com.itgrail.pilot.app.command.cmo.query.QueryRoleListCmdExe;
import com.itgrail.pilot.app.command.co.QueryPerminByRoleCO;
import com.itgrail.pilot.app.command.co.RoleCO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itgrail.grail.cqrs.command.CommandBus;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;

import java.util.List;

@RestController
@RequestMapping("/admin/role")
@Api("角色管理")
public class RoleController {


    @Autowired
    private CommandBus commandBus;


    @PostMapping("/page")
    @ApiOperation(value = "分页查询角色")
    @RequiresPermissions("role:page")
    public ResultData<PageResult<RoleCO>> getRolePage(@RequestBody PageQueryRoleCmd pageQueryRoleCmd) {
        return commandBus.send(pageQueryRoleCmd);
    }

    @GetMapping("/list")
    @ApiOperation(value = "角色列表")
    @RequiresPermissions("role:list")
    public ResultData<PageResult<RoleCO>> getRoleList() {
        return commandBus.send(QueryRoleListCmdExe.class);
    }


    @GetMapping("/queryPermByRole")
    @ApiOperation(value = "根据Role查询权限")
    @RequiresPermissions("role:queryPermByRole")
    public ResultData<List<QueryPerminByRoleCO>> queryPerminByRole(@RequestParam(name = "roleName",
            required = false) String roleName) {
        QueryPerminByRoleCmd perminByRoleCmd = new QueryPerminByRoleCmd();
        perminByRoleCmd.setRoleName(roleName);
        return commandBus.send(perminByRoleCmd);
    }


    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "增加或更新角色")
    @RequiresPermissions("role:addOrUpdate")
    public ResultData<Void> addOrUpdateRole(@RequestBody AddOrUpdateRoleCmd addOrUpdateRoleCmd) {
        return commandBus.send(addOrUpdateRoleCmd);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除角色")
    @RequiresPermissions("role:delete")
    public ResultData<Void> delete(Long id) {
        DeleteRoleByIdCmd deleteRoleByIdCmd = new DeleteRoleByIdCmd();
        deleteRoleByIdCmd.setId(id);
        return commandBus.send(deleteRoleByIdCmd);
    }

}
